/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */

/*Vue.transition('fade', {
    enterClass: 'fadeInLeft',
    leaveClass: 'hide'
});*/

var forword = encodeURIComponent(window.location.href);
var backUrl = $.getUrlVar("back");

var uid = $.getUrlVar("uid");

Vue.use(VueTouch);

var vm=new Vue({
  el:"#app",
  data:{
    fileInfo:[],
    fileList:{    //文档
      isLoading:true,
      pageSize:PAGESIZE,
      pageIndex:1
    },
    topicInfo:[],
    topicList:{   //话题
      isLoading:true,
      pageSize:PAGESIZE,
      pageIndex:1
    },
    answerInfo:[],
    answerList:{  //回答
      isLoading:true,
      pageSize:PAGESIZE,
      pageIndex:1
    },

    addShow: false,
    tabs:0,
    isEdit:'编辑',
    ableEdit:true,
    isDrift:false,

    inputText:'',
    showModal:false,
    shareObj:{
      dataID:'',
      shareTit:'',
      sharePic:''
    },
    whose:'我',
    selfUid:'',
    noList: false, //处理vm没数据的情况下显示“暂无数据”
    reqOut: false, //判断请求数据是否已完
  },
  created:function(){
    var _self = this;
    var data = {
      uid:uid
    }
    Common.ajaxCall({ url: 'user_getUserInfo', datatype: 'json', type: 'get'}, function(data) {
        if (data.error_code == 0) {
            _self.selfUid = data.data.uid;
        } else {
            alert(data.msg);
        }
    });
    Common.ajaxCall({ url: 'user_getUserInfo', datatype: 'json', type: 'get',data: data}, function(data) {
        if (data.error_code == 0) {
            if(uid && uid !== _self.selfUid){
              _self.whose = data.data.userName;
              _self.ableEdit = false;
            }

        } else {
            alert(data.msg);
        }
    });

  },
  compiled: function(){
    var _self = this;
    this.loadPhoto();
    var top = $(document).scrollTop();
    $(document).scroll(function () {
      if(top!=$(document).scrollTop()){
          // 上下滚动
          var docHeight = $(document).height(),
              winHeight = $(window).height();
          if ((docHeight > winHeight)&&($(document).scrollTop() >= docHeight - winHeight)) {
            if (!_self.reqOut) {
              _self.reqOut = true;
              var str = '<img class="loading-img" src="images/loading.gif"></img>';
              myLoad.pullLoad('quesCont', str);
              _self.loadlist(_self.tabs);

            }
          }
      }else{
          // 左右滚动
      }
      
    });
    
    $(document).on('change.inputText','.fillInput',function(){
        _self.inputText = $(this).val();
    });


    $(document).on('click','.edit',function(e){
      if($(e.target).hasClass('on')){
        $(e.target).removeClass('on');
      }else{
        $(e.target).addClass('on');
      }
    });

    _self.loadlist(_self.tabs);
  },
  ready:function(){

  },
  methods:{
    loadlist:function(tabs){
      var _self = this;
      var vModal,target;
      switch(tabs){
        case 0:
          var url = 'user_getCollectionArticleList';
          var defaults = {
            pageSize:_self.fileList.pageSize,
            pageIndex:_self.fileList.pageIndex
          } 

          var data = $.extend(defaults,{});

          vModal = _self.fileList;
          target = _self.fileInfo;
        break;
        case 1:
          var url = 'user_getCollectionAnswerList';
          var defaults = {
            pageSize:_self.answerList.pageSize,
            pageIndex:_self.answerList.pageIndex
          }; 
          var data = $.extend(defaults,{});

          vModal = _self.answerList;
          target = _self.answerInfo;
        break;
        case 2:
          var url = 'user_getCollectionTopicList';
          var defaults = {
            pageSize:_self.topicList.pageSize,
            pageIndex:_self.topicList.pageIndex
          }; 
          var data = $.extend(defaults,{});

          vModal = _self.topicList;
          target = _self.topicInfo;
        break;
      }
      if(uid){  //没uid的时候，查自己
        var data = $.extend(defaults,{uid:uid});
      }

      if(!vModal || !vModal.isLoading) return;
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: url, datatype: 'json', type: 'get', data: data}, function (data) {
          if(data.error_code==0 && data.data.length > 0){
              if(vModal.pageIndex==1){
                target.length = 0;
              }
              for(var i = 0; i < data.data.length; i++){
                target.push(data.data[i]);
              }
              _self.reqOut = false;
              
              //实际数据与请求的数据量比较
              if (data.data.length >= vModal.pageSize) {
                  myLoad.pullHide(); //加载后，把效果去掉
              } else {
                  _self.reqOut = true;
                  myLoad.noDate(); //暂无数据
                  vModal.isLoading = false;
              }
          }else{
              if (!(target.length > 0)) { _self.noList = true; } //处理整个vm没有数据的情况，显示“暂无数据”
              _self.reqOut = true;
              myLoad.noDate(); //暂无数据
              vModal.isLoading = false;
          }

          vModal.pageIndex++;
      });
      
    },
    loadPhoto:function(){
      // 轮播比例
      myJS.imgScale('.j_1', '1', '1');
      myJS.ImgFull('.loadImg','images/liby.png');
    },
    slides:function(index,e){
      var _self = this;
      
      if(_self.isDrift === true) return;  //如果处于左滑状态则不予切换
      if(_self.isEdit==='取消') return;  //如果处于编辑状态则不予切换
      //清除原来vm数据

      _self.tabs = index;
      $(e.currentTarget).addClass('on').siblings().removeClass('on');

      _self.fileInfo = [];
      _self.topicInfo = [];
      _self.answerInfo = [];
      

      _self.fileList = {    //文档
        isLoading:true,
        pageSize:PAGESIZE,
        pageIndex:1
      };

      _self.topicList = {   //话题
        isLoading:true,
        pageSize:PAGESIZE,
        pageIndex:1
      };

      _self.answerList = {  //回答
        isLoading:true,
        pageSize:PAGESIZE,
        pageIndex:1
      };
      _self.reqOut = false;
      
      _self.loadlist(_self.tabs);
      
    },
    swipeLeft: function (e) {
      var _self = this;
      if(uid) return;
      if(_self.isEdit==='取消') return;  //如果处于编辑状态则不予左滑

      var shareColl = $(e.target).parents('li');
      var touchDrift = ($(e.target).parents('li').width())/3;
      var drift = ($(e.target).parents('li').width())/2;
      //回复原位置
      _self.isDrift = false;  //标识为非左滑状态
      $(e.target).parents('ul').children().animate({left:0});
      $(e.target).parents('ul').children().find('.shareColl').animate({width:0,right:0});
      

      console.log(e.deltaX);
      if(e.deltaX<0){      //左滑
        _self.isDrift = true;  //标识为左滑状态
        if(e.deltaX < -touchDrift){
          $(e.target).parents('li').animate({left:-drift});
          var height = $(e.target).parents('li').outerHeight();
          shareColl.find('.shareColl').animate({width:drift,right:-drift}).show().css('line-height',height + 'px');
        }
      }else{               //右滑
        if(e.deltaX > touchDrift){
          _self.isDrift = false;  //标识为非左滑状态
          $(e.target).parents('li').animate({left:0});
          shareColl.find('.shareColl').fadeOut();
          
        }
      }
    },
    share:function(obj,e){
        $(e.target).parents('li').animate({left:0}).find('.shareColl').fadeOut(); //将左滑的元素复位
        var _self = this;
        _self.isDrift = false;  //分享后复原
        _self.showModal = true;
        console.log(obj);
        _self.shareObj = {
          dataID : obj.shareId, //文档id
          shareTit : obj.shareTit, //文档标题
          sharePic : obj.sharePic,    //文档图片
          isdoc : obj.isdoc   //是不是文档
        };
        
    },
    shareReg:function(userList){
        var _self = this;
        if(!userList){     //如果没选择分享对象回到原来界面
          _self.showModal = false;
          return;
        }
        _self.shareObj.isdoc ? scale="s_34" : scale="s_1";
        alert({
            close: false,
            text: '分享',
            description: '<div class="clearfix shareTxt"><span class="j_1 photo"><img class="'+ scale +'" src="'+Common.wordIcon(_self.shareObj.sharePic,_self.shareObj.isdoc) +'" onerror=\"javascript:this.src=\'images/icon/type/OTHER.png\';this.onerror=null;\"></span><span class="text">'+_self.shareObj.shareTit+'</span></div>',
            fillInput: true,
            kind: 2,
            okEvent: function(){
                var data = {
                    dataID: _self.shareObj.dataID,
                    userList : userList,
                    comment:_self.inputText,
                    type:1
                };
                data = Common.jsonEncode(data);
                Common.ajaxCall({ url: 'look_share', datatype: 'json', type: 'get', data: data}, function (data) {
                    if(data.error_code==0){
                        var obj = data.data[0];
                        var code = obj.code;
                        var pic = code==1?"moneyPig.png":"lebel.png";
                        alert({
                            text: '<img src="images/'+pic+'"/>',
                            description: obj.str,
                            kind: 0
                        });
                    }else{
                        alert(data.msg);
                    }
                });
            }
        });
    },
    edit:function(){    //编辑
      var _self = this;
      if(_self.isDrift === true) return;  //如果处于左滑状态则不予编辑
      var fontSize = parseInt($('html').css('fontSize'));  //获取当前屏幕的字体  
      var $li = $('.tabCont li');

      if(_self.isEdit==='编辑'){
        _self.isEdit = '取消';

        $li.animate({left:-2*fontSize});
        $('.edit').animate({right:-2*fontSize,width:2*fontSize}).show();
      }else{
        _self.isEdit = '编辑';

        $li.animate({left:0});
        $('.edit').animate({width:0}).hide();
      }
    },
    doDel:function(options){   //删除
      var _self = this;
      var dataID = [],type;

      /*将tab数与type数对应*/
      _self.tabs===0 ? type=1 : _self.tabs === 1 ? type=3 : type=2;
      
      //console.log($('.edit').hasClass('on'));
      [].forEach.call($('.edit'),function(val,index,context){
          var $val = $(val);
          if($val.hasClass('on')){
            dataID.push($val.parents('li').data('id'));
          }
      });
      if(options){
        var data = {
          dataID : options.dataID,
          type : type
        }
      }else{
        if(dataID.length === 0){
          alert('您没有作任何选择！');
          return;
        }
        var data = {
          dataID : dataID.join(','),
          type : type
        }
      }

      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'user_deleteCollection', datatype: 'json', type: 'get', data: data}, function (data) {
          if(data.error_code==0){
              alert(data.msg);

              _self.isEdit = '编辑'; //将状态复原
              _self.isDrift = false;

              _self.fileInfo = [];
              _self.topicInfo = [];
              _self.answerInfo = [];
              _self.fileList = {    //文档
                isLoading:true,
                pageSize:PAGESIZE,
                pageIndex:1
              };

              _self.topicList = {   //话题
                isLoading:true,
                pageSize:PAGESIZE,
                pageIndex:1
              };

              _self.answerList = {  //回答
                isLoading:true,
                pageSize:PAGESIZE,
                pageIndex:1
              };
              _self.loadlist(_self.tabs);
          }else{
              alert(data.msg);
          }
      });
    },
    chooseAll:function(){   //全选
      if($('.edit').not('.on').length===0){
        $('.edit').removeClass('on');
      }else{
        $('.edit').addClass('on');
      }
    },
    toDetail:function(type,e){
      var _self = this;
      if(_self.isDrift === true) return;  //如果处于左滑状态则不予跳转
      if(_self.isEdit==='取消') return;   //如果处于编辑状态则不予跳转
      console.log($(e));
      var id = $(e.target).parents('li').data('id')
      switch(type){
        case 1:
          window.location.href = "articleDetail.html?id=" + id + "&back=" + forword;
        break;
        case 2:
          window.location.href = "askDetail.html?id=" + id + "&back=" + forword;
        break;
        case 3:
          window.location.href = "circleDetail.html?id=" + id + "&back=" + forword;
        break;
        default :
        break;
      }
      
    },
    back:function(){
      window.location.href = decodeURIComponent(backUrl);
    }
  },
  watch:{
    "answerInfo,topicInfo": function(){
      this.loadPhoto();
    }
  }
});